#include "mycoin.h"

//MyCoin::MyCoin(QWidget *parent) : QPushButton(parent)
//{

//}

MyCoin::MyCoin(QString btnImg)
{
    QPixmap pix;
    bool ret = pix.load(btnImg);
    if(!ret) {
        QString str = QString("The path: %1 is invalid").arg(btnImg);
        cout << str;
        return;
    }

    this->setFixedSize(pix.width(), pix.height());
    this->setStyleSheet("QPushButton{border: 0px;}");
    this->setIcon(pix);
    this->setIconSize(QSize(pix.width(), pix.width()));

    // 初始化定时器对象
    timer1 = new QTimer(this);
    timer2 = new QTimer(this);

    // 监听正面翻反面的信号，并且翻转金币
    connect(timer1, &QTimer::timeout, [=]() {
        QPixmap pix;
        QString str = QString(":/res/Coin000%1").arg(this->min++);
        pix.load(str);

        this->setFixedSize(pix.width(), pix.height());
        this->setStyleSheet("QPushButton{border: 0px;}");
        this->setIcon(pix);
        this->setIconSize(QSize(pix.width(), pix.height()));

        // 判断如果翻完了，将min置为1
        if(this->min > this->max) {
            this->isAnimation = false;
            this->min = 1;
            timer1->stop();
        }
    });

    connect(timer2, &QTimer::timeout, [=]() {
        QPixmap pix;
        QString str = QString(":/res/Coin000%1").arg(this->max--);
        pix.load(str);

        this->setFixedSize(pix.width(), pix.height());
        this->setStyleSheet("QPushButton{border: 0px;}");
        this->setIcon(pix);
        this->setIconSize(QSize(pix.width(), pix.height()));

        // 判断如果翻完了，将min置为1
        if(this->max < this->min) {
            this->isAnimation = false;
            this->max = 8;
            timer2->stop();
        }
    });

    isWin = false;

}

// 改变正反标记的方法
void MyCoin::changeFlag()
{
    // 如果是正面，翻成反面
    if(this->flag) {
        timer1->start(30);
        this->isAnimation = true;
        this->flag = false;
    }else {
        timer2->start(30);
        this->isAnimation = true;
        this->flag = true;
    }

}

void MyCoin::mousePressEvent(QMouseEvent *e)
{
    if(this->isAnimation || this->isWin) {

        return;
    }else {
        return QPushButton::mousePressEvent(e);
    }

}
